/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.Color;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.mlc.swing.layout.LayoutConstraintsManager;

import slhs.tempo.TableAnalyzer.IntegerField;

/**
 * ErpProbeDataParentPanelHandler.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class ErpProbeDataParentPanelHandler extends JPanel implements PanelHandler {
    private static final String ERP_PROBE_DATA_PANEL_LAYOUT = "ERP_PROBE_DATA_PANEL_LAYOUT.xml";

    private JPanel erpProbeDataPanel;
    private AddMarksPopup addMarksPopup;

    // here are declarations for the controls you created
    private IntegerField numberOfMarksTextfield;
    private JTextField filePathTextField;
    private JTextField pauseBeforeProbetextField;
    private JLabel pauseBeforeProbeLabel;
    private JTextField probeTextField;
    private JLabel numberOfMarksLabel;
    private JComboBox expectedAnswerComboBox;
    private JButton addMarksButton;
    private JLabel expectedAnswerLabel;
    private JLabel probeLabel;
    private JLabel filePathLabel;
    private JButton browseButton;

    public ErpProbeDataParentPanelHandler() {
	initComponents();
	addComponents();

    }

    /**
     * 
     */
    private void addComponents() {

	LayoutConstraintsManager layoutConstraintsManager = LayoutConstraintsManager.getLayoutConstraintsManager(this
		.getClass().getResourceAsStream(ERP_PROBE_DATA_PANEL_LAYOUT));
	LayoutManager panelLayout = layoutConstraintsManager.createLayout("panel", this);
	erpProbeDataPanel.setLayout(panelLayout);

	// here we add the controls to the container.
	erpProbeDataPanel.add(numberOfMarksTextfield, "numberOfMarksTextfield");
	erpProbeDataPanel.add(filePathTextField, "filePathTextField");
	erpProbeDataPanel.add(pauseBeforeProbetextField, "pauseBeforeProbetextField");
	erpProbeDataPanel.add(pauseBeforeProbeLabel, "pauseBeforeProbeLabel");
	erpProbeDataPanel.add(probeTextField, "probeTextField");
	erpProbeDataPanel.add(numberOfMarksLabel, "numberOfMarksLabel");
	erpProbeDataPanel.add(expectedAnswerComboBox, "expectedAnswerComboBox");
	erpProbeDataPanel.add(addMarksButton, "addMarksButton");
	erpProbeDataPanel.add(expectedAnswerLabel, "expectedAnswerLabel");
	erpProbeDataPanel.add(probeLabel, "probeLabel");
	erpProbeDataPanel.add(filePathLabel, "filePathLabel");
	erpProbeDataPanel.add(browseButton, "browseButton");

    }

    /**
     * 
     */
    private void initComponents() {
	erpProbeDataPanel = new JPanel();
	addMarksPopup = new AddMarksPopup();

	// here are declarations for the controls you created
	numberOfMarksTextfield = new IntegerField();
	filePathTextField = new JTextField();
	pauseBeforeProbetextField = new JTextField();
	pauseBeforeProbeLabel = new JLabel("Pause Before Probe (msec.)");
	probeTextField = new JTextField();
	numberOfMarksLabel = new JLabel("# of Marks");
	expectedAnswerComboBox = new JComboBox(UtilityConstants.EXPECTED_ANSWERS);
	addMarksButton = new JButton("Add Marks");
	expectedAnswerLabel = new JLabel("Expected Answer");
	probeLabel = new JLabel("Probe");
	filePathLabel = new JLabel("File Path");
	browseButton = new JButton("Browse");

	// configure components
	// addMarksButton.setEnabled(false);
	addMarksButton.addActionListener(this);
	browseButton.addActionListener(this);
	numberOfMarksTextfield.addActionListener(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
	System.out.println("Event ocured");

	Object source = event.getSource();

	if (source.equals(addMarksButton)) {
	    System.out.println("Pressed button");
	    String numberOfMarks = numberOfMarksTextfield.getText();
	    System.out.println("number of marks:\t" + numberOfMarks);
	    if ((numberOfMarks.equals("")) || (numberOfMarks == null)) {
		JOptionPane.showMessageDialog(null, "Specify number of marks");
		numberOfMarksTextfield.setBackground(Color.YELLOW);
	    } else {
		addMarksPopup.setVisible(true, numberOfMarks);

	    }
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	// TODO Auto-generated method stub
	return erpProbeDataPanel;
    }

    /**
     * @param string
     */
    protected void setPauseBeforeProbeLabel(String newLabel) {
	pauseBeforeProbeLabel.setText(newLabel);
    }

}
